﻿using System.Collections.Generic;
using System.Xml;
using Common.Utilities.Excel.Xml;

namespace Common.Utilities.Excel.Common
{
    /// <remarks>
    /// 静态区域
    /// </remarks>
    public class StaticArea : Area
    {
        /// <summary>
        /// 静态区域里的所有单元格
        /// </summary>
        public List<DataCell> DataCells { get; set; }

        /// <summary>
        /// 构建函数
        /// </summary>
        /// <param name="areaNode"></param>
        /// <param name="worksheet"></param>
        public StaticArea(XmlNode areaNode, WorksheetBase worksheet)
            : base(areaNode, worksheet)
        {
            this.SetStaticAreaXmlNode(areaNode);
        }

        /// <summary>
        /// 静态区域, 不是重复所以返回false
        /// </summary>
        public override bool IsRepeatArea
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// 设置XML配置
        /// </summary>
        /// <param name="areaNode"></param>
        public override void SetXmlNode(XmlNode areaNode)
        {
            base.SetXmlNode(areaNode);
            this.SetStaticAreaXmlNode(areaNode);
        }

        #region 私有方法

        /// <summary>
        /// 设置静态区域XML配置
        /// </summary>
        /// <param name="areaNode"></param>
        private void SetStaticAreaXmlNode(XmlNode areaNode)
        {
            // 静态跨行数
            RowSpan = XmlUtility.getNodeAttributeIntValue(areaNode, "rowspan");
            // 单元格
            DataCells = GetCells(areaNode);
        }

        /// <summary>
        /// 获取所有属于此区域的单元格
        /// </summary>
        /// <param name="areaNode"></param>
        /// <returns></returns>
        private List<DataCell> GetCells(XmlNode areaNode)
        {
            List<DataCell> cells = new List<DataCell>();
            XmlNodeList cellNodes = areaNode.ChildNodes;
            if (cellNodes != null && cellNodes.Count > 0)
                foreach (XmlNode cellNode in cellNodes)
                {
                    if (cellNode.Name.ToLower() == "cell")
                    {
                        DataCell cell = new DataCell(cellNode, this);
                        cells.Add(cell);
                    }
                }
            return cells;
        }

        #endregion
    }
}
